﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using BusinessLayer.Pos;
using System.IO;
using System.Text;
using Microsoft.Win32;
using System.Reflection;


namespace CrmControls
{
    [Guid("dffd4630-66f6-415a-8178-fb75e9f00bda")]
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public partial class PosCashSend : UserControl
    {
        Dictionary<int, int> depoId;
        Servis.DataService srv;
        public PosCashSend()
        {
            InitializeComponent();
            depoId = new Dictionary<int, int>();
            srv = new CrmControls.Servis.DataService();
            srv.Url = "http://localhost/Crm/DataService.asmx";
        }

        [ComRegisterFunction()]
        public static void RegisterClass(string key)
        {
            // Strip off HKEY_CLASSES_ROOT\ from the passed key as I don't need it
            StringBuilder sb = new StringBuilder(key);

            sb.Replace(@"HKEY_CLASSES_ROOT\", "");
            // Open the CLSID\{guid} key for write access
            RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);

            // And create	the	'Control' key -	this allows	it to show up in
            // the ActiveX control container
            RegistryKey ctrl = k.CreateSubKey("Control");
            ctrl.Close();

            // Next create the CodeBase entry	- needed if	not	string named and GACced.
            RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true);
            inprocServer32.SetValue("CodeBase", Assembly.GetExecutingAssembly().CodeBase);
            inprocServer32.Close();
            // Finally close the main	key
            k.Close();
            MessageBox.Show("Registered");
        }

        [ComUnregisterFunction()]
        public static void UnregisterClass(string key)
        {
            StringBuilder sb = new StringBuilder(key);
            sb.Replace(@"HKEY_CLASSES_ROOT\", "");

            // Open	HKCR\CLSID\{guid} for write	access
            RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);

            // Delete the 'Control'	key, but don't throw an	exception if it	does not exist
            k.DeleteSubKey("Control", false);

            // Next	open up	InprocServer32
            //RegistryKey	inprocServer32 = 
            k.OpenSubKey("InprocServer32", true);

            // And delete the CodeBase key,	again not throwing if missing
            k.DeleteSubKey("CodeBase", false);

            // Finally close the main key
            k.Close();
            MessageBox.Show("UnRegistered");
        }


        private void button1_Click(object sender, EventArgs e)
        {
            switch (cmbBilgi.SelectedIndex)
            {
                case 0:
                    CariGonder();
                    break;
                case 1:
                    StokFiyatGonder();
                    break;
                default:
                    break;
            }

        }

        private void StokFiyatGonder()
        {
            if (cmbDepo.SelectedIndex > -1)
            {
                DataSet ds = srv.PosList2(depoId[cmbDepo.SelectedIndex], 0); //kasa tipi inter olanlar
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        List<string> mesajyollari = new List<string>();
                        List<string> cariyollari = new List<string>();

                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            cariyollari.Add(ds.Tables[0].Rows[i]["cash_stok_exp"].ToString());
                            mesajyollari.Add(ds.Tables[0].Rows[i]["cash_msg"].ToString());
                        }

                        DataSet cariDt = srv.BackOfficeFiyatlar(depoId[cmbDepo.SelectedIndex].ToString(), int.Parse(txtfiltre.Text));
                        InterMpos2001 pos = new InterMpos2001();
                        if (cariDt.Tables.Count > 0)
                        {
                            pos.UrunTaksit(cariDt.Tables[0], cariyollari.ToArray());
                        }
                        pos.MesajYaz(false, false, true, false, mesajyollari.ToArray());
                        MessageBox.Show("Aktarım Tamamlandı !");
                    }
                }
            }
            else
            {
                MessageBox.Show("Lütfen Depoyu Seçin !");
            }
        }

        private void CariGonder()
        {
            if (cmbDepo.SelectedIndex > -1)
            {
                DataSet ds = srv.PosList2(depoId[cmbDepo.SelectedIndex], 0); //kasa tipi inter olanlar
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        List<string> mesajyollari = new List<string>();
                        List<string> cariyollari = new List<string>();

                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            cariyollari.Add(ds.Tables[0].Rows[i]["cash_stok_exp"].ToString());
                            mesajyollari.Add(ds.Tables[0].Rows[i]["cash_msg"].ToString());
                        }

                        DataSet cariDt = srv.MusteriList();
                        InterMpos2001 pos = new InterMpos2001();
                        if (cariDt.Tables.Count > 0)
                        {
                            pos.InterPOS_Cari_Exp(cariDt.Tables[0], cariyollari.ToArray());
                        }
                        pos.MesajYaz(false, false, false, true, mesajyollari.ToArray());
                        MessageBox.Show("Aktarım Tamamlandı !");
                    }
                }
            }
            else
            {
                MessageBox.Show("Lütfen Depoyu Seçin !");
            }
        }

        private void PosCashSend_Load(object sender, EventArgs e)
        {
            DataSet depolar = srv.DepoList();
            if (depolar.Tables.Count > 0)
            {
                for (int i = 0; i < depolar.Tables[0].Rows.Count; i++)
                {
                    depoId.Add(i, Convert.ToInt32(depolar.Tables[0].Rows[i][0]));
                    cmbDepo.Items.Add(depolar.Tables[0].Rows[i][1].ToString());
                }
            }
            cmbBilgi.SelectedIndex = 0;
            cmbFiltre.Enabled = false;
            txtfiltre.Enabled = false;
        }

        private void cmbBilgi_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbBilgi.SelectedIndex == 1)
            {
                cmbFiltre.Enabled = true;
                txtfiltre.Enabled = true;
            }
            else
            {
                cmbFiltre.Enabled = false;
                txtfiltre.Enabled = false;
            }
        }
    }
}
